console.log("loading drawCubeBox -> ");

const vs = `
attribute vec4 a_Position;
attribute vec4 a_Color;
uniform mat4 u_ViewMatrix;
uniform mat4 u_ModelMatrix;
uniform mat4 u_ModelViewMatrix;
varying vec4 v_Color;
void main() {
    gl_Position = u_ViewMatrix *a_Position;
    v_Color = a_Color;
}`;


const ps = `
#ifdef GL_ES
precision mediump float;
#endif
varying vec4 v_Color;
void main() {
    gl_FragColor=v_Color;
}`;


function initVertexBuffers(gl) {

    let vertices = new Float32Array([
        -0.0,  0.5,  -0.4, 0.4, 1.0, 0.4,
        -0.5, -0.5,  -0.4, 0.4, 1.0, 0.4,
         0.5, -0.5,  -0.4, 1.0, 0.4, 0.4,

        0.5, 0.4, -0.2, 1.0, 0.4, 0.4,
        -0.5, 0.4, -0.2, 1.0, 1.0, 0.4,
        0.0, -0.6, -0.2, 1.0, 1.0, 0.4,

        0.0, 0.5, 0.0, 0.4, 0.4, 1.0,
        -0.5, -0.5, 0.0, 0.4, 0.4, 1.0,
        0.5, -0.5, 0.0, 1.0, 0.4, 0.4,
    ]);

    let n = 9;

    // 
    let vertexBuffer = gl.createBuffer();
    if (!vertexBuffer) {
        console.error("failed create vertexBuffer");
        return;
    }
    gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
    gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);
    let FSIZE = vertices.BYTES_PER_ELEMENT;
    console.log("FSIZE",FSIZE)
    let a_Position = gl.getAttribLocation(gl.program, 'a_Position');
    if (a_Position < 0) {
        console.error("Failed to get locaiton of a_Position");
        return -1;
    }
    gl.vertexAttribPointer(a_Position, 3, gl.FLOAT, false, FSIZE * 6, 0);
    gl.enableVertexAttribArray(a_Position);

    let a_Color = gl.getAttribLocation(gl.program, 'a_Color');
    if (a_Color < 0) {
        console.error("failed to get location of a_Color");
        return -1;
    }
    gl.vertexAttribPointer(a_Color, 3, gl.FLOAT, false, FSIZE * 6, FSIZE * 3);
    gl.enableVertexAttribArray(a_Color);
    gl.bindBuffer(gl.ARRAY_BUFFER, null);

    return n;


}

function main() {

    let canvas = document.getElementById('glCanvas');
    let gl = canvas.getContext('webgl');
    if (!gl) {
        console.error("Failed get gl");
        return;
    }

    gl.viewport(0, 0, canvas.width, canvas.height)
    if (!initShaders(gl, vs, ps)) {
        console.error("Failed init shaders");
        return;
    }

    let vertexNum = initVertexBuffers(gl);
    if (vertexNum < 0) {
        console.error("Failed initVertexBuffers");
        return;
    }

    gl.clearColor(0.5, 0.5, 0.5, 1);

    let viewMatrix = new Matrix4();
    viewMatrix.setLookAt(0.20, 0.25, 0.25, 0, 0, 0, 0, 1, 0);

    // let modelMatrix = new Matrix4();
    // modelMatrix.setRotate(70,0,1,0);
    // gl.uniformMatrix4fv(u_ModelMatrix, false, modelMatrix.elements);

    let u_ViewMatrix = gl.getUniformLocation(gl.program,'u_ViewMatrix');
    if(!u_ViewMatrix){
        console.error("Failed to get location of u_ViewMatrix");
        return;
    }

    document.onkeydown = function(env){
        keydown(env,gl,vertexNum,u_ViewMatrix,viewMatrix);
    }
    // let modelViewMatrix = viewMatrix.multiply(modelMatrix);
    // gl.uniformMatrix4fv(u_ModelViewMatrix,false,modelViewMatrix.elements);

    gl.clear(gl.COLOR_BUFFER_BIT);
    gl.drawArrays(gl.TRIANGLES, 0, vertexNum);

}

let g_EyeX = 0.20;
let g_EyeY = 0.25;
let g_EyeZ = 0.25;

function keydown(env,gl,n,u_ViewMatrix,viewMatrix){
    console.log("keyCode = ",env.keyCode)
    if(env.keyCode == 39){
        g_EyeX += 0.01;
    }else if (env.keyCode == 37){
        g_EyeX -=0.01;
    }else{
        return;
    }

    draw(gl,n,u_ViewMatrix,viewMatrix);
}

function draw(gl,n,u_ViewMatrix,viewMatrix){

    viewMatrix.setLookAt(g_EyeX,g_EyeY,g_EyeZ,0,0,0,0,1,0);
    gl.uniformMatrix4fv(u_ViewMatrix,false,viewMatrix.elements);
    gl.clear(gl.COLOR_BUFFER_BIT);
    gl.drawArrays(gl.TRIANGLES,0,n);

}